/**************************************************************************/
/*! 
    @file     tsl2550.h
    @author   K. Townsend (microBuilder.eu)

    @section LICENSE

    Software License Agreement (BSD License)

    Copyright (c) 2010, microBuilder SARL
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/

#ifndef _TSL2550_H_
#define _TSL2550_H_

#include "../src/projectconfig.h"
#include "../core/i2c.h"

#define TSL2550_PACKAGE_CS                  // Lux calculations differ slightly for CS package
// #define TSL2550_PACKAGE_T_FN_CL

#define TSL2550_ADDRESS           (0x72)    // 0111001 shifted left 1 bit = 0x72 (ADDR = GND or floating)
#define TSL2550_READBIT           (0x01)


#define TSL2550_CONTROL_POWERON   (0x03)
#define TSL2550_CONTROL_POWEROFF  (0x00)
#define TSL2550_EXTENDED_RANGE    (0x1D)
#define TSL2550_STANDART_RANGE    (0x18)
#define TSL2550_RD_ADC0			  (0x43)
#define TSL2550_RD_ADC1			  (0x83)

// T, FN and CL package values
#define TSL2550_LUX_K1T           (0x0040)  // 0.125 * 2^RATIO_SCALE
#define TSL2550_LUX_B1T           (0x01f2)  // 0.0304 * 2^LUX_SCALE
#define TSL2550_LUX_M1T           (0x01be)  // 0.0272 * 2^LUX_SCALE
#define TSL2550_LUX_K2T           (0x0080)  // 0.250 * 2^RATIO_SCALE
#define TSL2550_LUX_B2T           (0x0214)  // 0.0325 * 2^LUX_SCALE
#define TSL2550_LUX_M2T           (0x02d1)  // 0.0440 * 2^LUX_SCALE
#define TSL2550_LUX_K3T           (0x00c0)  // 0.375 * 2^RATIO_SCALE
#define TSL2550_LUX_B3T           (0x023f)  // 0.0351 * 2^LUX_SCALE
#define TSL2550_LUX_M3T           (0x037b)  // 0.0544 * 2^LUX_SCALE
#define TSL2550_LUX_K4T           (0x0100)  // 0.50 * 2^RATIO_SCALE
#define TSL2550_LUX_B4T           (0x0270)  // 0.0381 * 2^LUX_SCALE
#define TSL2550_LUX_M4T           (0x03fe)  // 0.0624 * 2^LUX_SCALE
#define TSL2550_LUX_K5T           (0x0138)  // 0.61 * 2^RATIO_SCALE
#define TSL2550_LUX_B5T           (0x016f)  // 0.0224 * 2^LUX_SCALE
#define TSL2550_LUX_M5T           (0x01fc)  // 0.0310 * 2^LUX_SCALE
#define TSL2550_LUX_K6T           (0x019a)  // 0.80 * 2^RATIO_SCALE
#define TSL2550_LUX_B6T           (0x00d2)  // 0.0128 * 2^LUX_SCALE
#define TSL2550_LUX_M6T           (0x00fb)  // 0.0153 * 2^LUX_SCALE
#define TSL2550_LUX_K7T           (0x029a)  // 1.3 * 2^RATIO_SCALE
#define TSL2550_LUX_B7T           (0x0018)  // 0.00146 * 2^LUX_SCALE
#define TSL2550_LUX_M7T           (0x0012)  // 0.00112 * 2^LUX_SCALE
#define TSL2550_LUX_K8T           (0x029a)  // 1.3 * 2^RATIO_SCALE
#define TSL2550_LUX_B8T           (0x0000)  // 0.000 * 2^LUX_SCALE
#define TSL2550_LUX_M8T           (0x0000)  // 0.000 * 2^LUX_SCALE

// CS package values
#define TSL2550_LUX_K1C           (0x0043)  // 0.130 * 2^RATIO_SCALE
#define TSL2550_LUX_B1C           (0x0204)  // 0.0315 * 2^LUX_SCALE
#define TSL2550_LUX_M1C           (0x01ad)  // 0.0262 * 2^LUX_SCALE
#define TSL2550_LUX_K2C           (0x0085)  // 0.260 * 2^RATIO_SCALE
#define TSL2550_LUX_B2C           (0x0228)  // 0.0337 * 2^LUX_SCALE
#define TSL2550_LUX_M2C           (0x02c1)  // 0.0430 * 2^LUX_SCALE
#define TSL2550_LUX_K3C           (0x00c8)  // 0.390 * 2^RATIO_SCALE
#define TSL2550_LUX_B3C           (0x0253)  // 0.0363 * 2^LUX_SCALE
#define TSL2550_LUX_M3C           (0x0363)  // 0.0529 * 2^LUX_SCALE
#define TSL2550_LUX_K4C           (0x010a)  // 0.520 * 2^RATIO_SCALE
#define TSL2550_LUX_B4C           (0x0282)  // 0.0392 * 2^LUX_SCALE
#define TSL2550_LUX_M4C           (0x03df)  // 0.0605 * 2^LUX_SCALE
#define TSL2550_LUX_K5C           (0x014d)  // 0.65 * 2^RATIO_SCALE
#define TSL2550_LUX_B5C           (0x0177)  // 0.0229 * 2^LUX_SCALE
#define TSL2550_LUX_M5C           (0x01dd)  // 0.0291 * 2^LUX_SCALE
#define TSL2550_LUX_K6C           (0x019a)  // 0.80 * 2^RATIO_SCALE
#define TSL2550_LUX_B6C           (0x0101)  // 0.0157 * 2^LUX_SCALE
#define TSL2550_LUX_M6C           (0x0127)  // 0.0180 * 2^LUX_SCALE
#define TSL2550_LUX_K7C           (0x029a)  // 1.3 * 2^RATIO_SCALE
#define TSL2550_LUX_B7C           (0x0037)  // 0.00338 * 2^LUX_SCALE
#define TSL2550_LUX_M7C           (0x002b)  // 0.00260 * 2^LUX_SCALE
#define TSL2550_LUX_K8C           (0x029a)  // 1.3 * 2^RATIO_SCALE
#define TSL2550_LUX_B8C           (0x0000)  // 0.000 * 2^LUX_SCALE
#define TSL2550_LUX_M8C           (0x0000)  // 0.000 * 2^LUX_SCALE

#define TSL2550_MAX_LUX         1846

 static const uint8_t ratio_lut[] = {
         100, 100, 100, 100, 100, 100, 100, 100,
         100, 100, 100, 100, 100, 100, 99, 99,
         99, 99, 99, 99, 99, 99, 99, 99,
         99, 99, 99, 98, 98, 98, 98, 98,
         98, 98, 97, 97, 97, 97, 97, 96,
         96, 96, 96, 95, 95, 95, 94, 94,
         93, 93, 93, 92, 92, 91, 91, 90,
         89, 89, 88, 87, 87, 86, 85, 84,
         83, 82, 81, 80, 79, 78, 77, 75,
         74, 73, 71, 69, 68, 66, 64, 62,
         60, 58, 56, 54, 52, 49, 47, 44,
         42, 41, 40, 40, 39, 39, 38, 38,
         37, 37, 37, 36, 36, 36, 35, 35,
         35, 35, 34, 34, 34, 34, 33, 33,
         33, 33, 32, 32, 32, 32, 32, 31,
         31, 31, 31, 31, 30, 30, 30, 30,
         30,
 };

 static const uint16_t count_lut[] = {
         0, 1, 2, 3, 4, 5, 6, 7,
         8, 9, 10, 11, 12, 13, 14, 15,
         16, 18, 20, 22, 24, 26, 28, 30,
         32, 34, 36, 38, 40, 42, 44, 46,
         49, 53, 57, 61, 65, 69, 73, 77,
         81, 85, 89, 93, 97, 101, 105, 109,
         115, 123, 131, 139, 147, 155, 163, 171,
         179, 187, 195, 203, 211, 219, 227, 235,
         247, 263, 279, 295, 311, 327, 343, 359,
         375, 391, 407, 423, 439, 455, 471, 487,
         511, 543, 575, 607, 639, 671, 703, 735,
         767, 799, 831, 863, 895, 927, 959, 991,
         1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
         1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
         2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
         3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
 };


enum
{
  TSL2550_REGISTER_CONTROL          = 0x00,
};

typedef enum
{
  TSL2550_INTEGRATIONTIME_13MS      = 0x00,    // 13.7ms
  TSL2550_INTEGRATIONTIME_101MS     = 0x01,    // 101ms
  TSL2550_INTEGRATIONTIME_402MS     = 0x02     // 402ms
}
tsl2550IntegrationTime_t;
//
//typedef enum
//{
//  TSL2550_GAIN_0X                   = 0x00,    // No gain
//  TSL2550_GAIN_16X                  = 0x10,    // 16x gain
//}
//tsl2550Gain_t;

typedef enum
{
  TSL2550_ERROR_OK = 0,               // Everything executed normally
  TSL2550_ERROR_I2CINIT,              // Unable to initialise I2C
  TSL2550_ERROR_I2CBUSY,              // I2C already in use
  TSL2550_ERROR_LAST
}
tsl2550Error_t;

tsl2550Error_t tsl2550Init(void);
//tsl2550Error_t tsl2550GetLuminosity (uint8_t *broadband, uint8_t *ir);
//uint32_t tsl2550CalculateLux(uint8_t ch0, uint8_t ch1);
uint32_t tsl2550GetLux();

#endif


